//
//  ChatVM.m
//  WanCaiC8
//
//  Created by tangang on 2018/4/10.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "ChatVM.h"
#import "SocketRocketUtility.h"
@implementation ChatVM

- (void)postImageWithImage:(UIImage *)image andSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    
    [self POST_Field_Url:@"api/Talking/SaveImg" Params:@{@"img":[TGTools base64WithImage:image]} successBlock:^(id returnValue) {
        if ([[returnValue objectForKey:@"Code"] intValue]==APIResponseStutusSuccess) {
            success([returnValue objectForKey:@"Data"]);
        }else{
            [SVProgressHUD showErrorWithStatus:[returnValue objectForKey:@"Desc"]];
            fail(nil,nil);
        }
    } failBlock:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)addMessageWithModel:(ZXMessageModel *)model andRoomId:(NSString *)roomId{
    NSString *content=[[SocketRocketUtility instance] sendMessageConversion:model.text];
    if (model.messageType==ZXMessageTypeImage) {
        content=model.imageURL;
    }
    [self POST_Field_Url:@"api/Talking/AddMessage" Params:@{@"Content":content,@"UserId":model.from.UserId,@"UserName":[UserModel userModel].NickName,@"PhotoImg":model.from.Avater,@"RoomId":roomId,@"MsgTypeChild":@(model.messageType),@"Guid":model.Guid,@"IsAdmin":(model.isAdmin?@"True":@"False"),@"MasterLottery":model.from.MasterLottery} successBlock:^(id returnValue) {
        
    } failBlock:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)deleteMessageWithModel:(ZXMessageModel *)model andRoomId:(NSString *)roomId andSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    [self POST_Field_Url:@"api/Talking/DelMessage" Params:@{@"guid":model.Guid,@"userId":model.from.UserId,@"userName":model.from.NickName,@"roomId":roomId,@"ProcessId":[UserModel userModel].UserId,@"ProcessName":[UserModel userModel].NickName} successBlock:^(id returnValue) {
        if ([[returnValue objectForKey:@"Code"] intValue]==APIResponseStutusSuccess) {
            success(returnValue);
        }else{
            [SVProgressHUD showErrorWithStatus:[returnValue objectForKey:@"Desc"]];
            fail(nil,nil);
        }
    } failBlock:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD showErrorWithStatus:errorMessage];
        fail(errorMessage,error);
    }];
}

- (void)blackPeopleWithModel:(ZXMessageModel *)model andRoomId:(NSString *)roomId andSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    [self POST_Field_Url:@"api/Talking/AddBlackList" Params:@{@"userId":model.from.UserId,@"userName":model.from.NickName,@"roomId":roomId,@"ProcessId":[UserModel userModel].UserId,@"ProcessName":[UserModel userModel].NickName} successBlock:^(id returnValue) {
        success(returnValue);
    } failBlock:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD showErrorWithStatus:errorMessage];
        fail(errorMessage,error);
    }];
}

- (void)getHistoryWithRoomId:(NSString *)roomId andLastId:(NSString *)lastId andSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    [self GET_Field_Url:@"api/Talking/GetMessageList" Params:[[NSDictionary alloc]initWithObjectsAndKeys:roomId,@"roomId",lastId,@"guid", nil] successBlock:^(id returnValue) {
        if ([[returnValue objectForKey:@"Code"] intValue]==APIResponseStutusSuccess) {
            NSArray *arr=[returnValue objectForKey:@"Data"];
            NSMutableArray *dataArr=[[NSMutableArray alloc]init];
            for (NSDictionary *dict in arr) {
                ZXMessageModel *model=[ZXMessageModel modelWithMessageDict:dict];
                if (model.messageType==ZXMessageTypeText) {
                    model.text=[[SocketRocketUtility instance] getMessageConversion:model.text];
                }
                
                [dataArr insertObject:model atIndex:0];
            }
            success(dataArr);
        }else{
            [SVProgressHUD showErrorWithStatus:[returnValue objectForKey:@"Desc"]];
            fail(nil,nil);
        }
        
        
    } failBlock:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD showErrorWithStatus:errorMessage];
        fail(errorMessage,error);
    }];
}

- (void)deleteAllMessageWithModel:(ZXMessageModel *)model andRoomId:(NSString *)roomId andSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    [self POST_Field_Url:@"api/Talking/DelMessageAll" Params:@{@"userId":model.from.UserId,@"userName":model.from.NickName,@"roomId":roomId,@"ProcessId":[UserModel userModel].UserId,@"ProcessName":[UserModel userModel].NickName} successBlock:^(id returnValue) {
        if ([[returnValue objectForKey:@"Code"] intValue]==APIResponseStutusSuccess) {
            success(returnValue);
        }else{
            [SVProgressHUD showErrorWithStatus:[returnValue objectForKey:@"Desc"]];
            fail(nil,nil);
        }
    } failBlock:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD showErrorWithStatus:errorMessage];
        fail(errorMessage,error);
    }];
}

- (void)getRecordWithId:(NSString *)lastId andRoomId:(NSString *)roomId andSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    [self GET_Field_Url:@"api/Talking/GetProcessingRecords" Params:[[NSDictionary alloc]initWithObjectsAndKeys:roomId,@"roomId",lastId,@"id", nil] successBlock:^(id returnValue) {
        if ([[returnValue objectForKey:@"Code"] intValue]==APIResponseStutusSuccess) {
            NSArray *arr=[returnValue objectForKey:@"Data"];
            success([ChatRecordModel mj_objectArrayWithKeyValuesArray:arr]);
        }else{
            [SVProgressHUD showErrorWithStatus:[returnValue objectForKey:@"Desc"]];
            fail(nil,nil);
        }
        
    } failBlock:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD showErrorWithStatus:errorMessage];
        fail(errorMessage,error);
    }];
}

- (void)getBlackListWithId:(NSInteger )lastId andRoomId:(NSString *)roomId andSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    [self GET_Field_Url:@"api/Talking/GetBlackList" Params:[[NSDictionary alloc]initWithObjectsAndKeys:roomId,@"roomId",[NSNumber numberWithInteger:lastId],@"id", nil] successBlock:^(id returnValue) {
        if ([[returnValue objectForKey:@"Code"] intValue]==APIResponseStutusSuccess) {
            NSArray *arr=[returnValue objectForKey:@"Data"];
            success([BlackPeopleModel mj_objectArrayWithKeyValuesArray:arr]);
        }else{
            [SVProgressHUD showErrorWithStatus:[returnValue objectForKey:@"Desc"]];
            fail(nil,nil);
        }
        
    } failBlock:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD showErrorWithStatus:errorMessage];
        fail(errorMessage,error);
    }];
}

- (void)getBlackListRoomId:(NSString *)roomId andSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    [self GET_Field_Url:@"api/Talking/GetBlackListStr" Params:[[NSDictionary alloc]initWithObjectsAndKeys:roomId,@"roomId", nil] successBlock:^(id returnValue) {
        if ([[returnValue objectForKey:@"Code"] intValue]==APIResponseStutusSuccess) {
            success([returnValue objectForKey:@"Data"]);
        }else{
            [SVProgressHUD showErrorWithStatus:[returnValue objectForKey:@"Desc"]];
            fail(nil,nil);
        }
    } failBlock:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD showErrorWithStatus:errorMessage];
        fail(errorMessage,error);
    }];
}

- (void)removeBlackWithId:(NSString *)userId andRoomId:(NSString *)roomId andUserName:(NSString *)userName andSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    [self POST_Field_Url:@"api/Talking/RemoveBlackList" Params:[[NSDictionary alloc]initWithObjectsAndKeys:roomId,@"roomId",userId,@"userId",userName,@"userName",[UserModel userModel].UserId,@"ProcessId",[UserModel userModel].NickName,@"ProcessName", nil] successBlock:^(id returnValue) {
        if ([[returnValue objectForKey:@"Code"] intValue]==APIResponseStutusSuccess) {
            success([returnValue objectForKey:@"Data"]);
        }else{
            [SVProgressHUD showErrorWithStatus:[returnValue objectForKey:@"Desc"]];
            fail(nil,nil);
        }
    } failBlock:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD showErrorWithStatus:errorMessage];
        fail(errorMessage,error);
    }];
}

- (void)getRoomListWithSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    [self GET_Field_Url:@"api/Talking/GetChatRoomList" Params:nil successBlock:^(id returnValue) {
        if ([[returnValue objectForKey:@"Code"] intValue]==APIResponseStutusSuccess) {
            success([RoomModel mj_objectArrayWithKeyValuesArray:[returnValue objectForKey:@"Data"]]);
        }else{
            [SVProgressHUD showErrorWithStatus:[returnValue objectForKey:@"Desc"]];
            fail(nil,nil);
        }
    } failBlock:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD showErrorWithStatus:errorMessage];
        fail(errorMessage,error);
    }];
}

- (void)getRoomStateWithSuccess:(SuccessBlock)success andFail:(FailBlock)fail{
    [self GET_Field_Url:@"api/Talking/GetUserState" Params:nil successBlock:^(id returnValue) {
        if ([[returnValue objectForKey:@"Code"] intValue]==APIResponseStutusSuccess) {
            NSDictionary *dict=[returnValue objectForKey:@"Data"];
            [UserInfoModel share].isAdmin=[[dict objectForKey:@"IsChatAD"] boolValue];
            [UserInfoModel share].isBlack=[[dict objectForKey:@"ChatBlack"] boolValue];
            [UserModel userModel].MasterLottery=[dict objectForKey:@"MasterLottery"];
            success([RoomModel mj_objectArrayWithKeyValuesArray:[returnValue objectForKey:@"Data"]]);
        }else{
            [SVProgressHUD showErrorWithStatus:[returnValue objectForKey:@"Desc"]];
            fail(nil,nil);
        }
    } failBlock:^(NSString *errorMessage, NSError *error) {
        [SVProgressHUD showErrorWithStatus:errorMessage];
        fail(errorMessage,error);
    }];
}

static ChatVM *n_manager=nil;
+ (ChatVM *)manage{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        n_manager=[[ChatVM alloc]init];
    });
    return n_manager;
}
@end

@implementation BlackPeopleModel
@end
